package com.daiqee.interceptor;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 用户登录记录
 * @author RichardT
 * 
 * <br/>
 * @since JDK 1.8
 * @version  V2.1
 * @author RichardTang 
 * @date: 2018年5月7日 下午5:58:09
 * @package com.daiqee.interceptor
 * @copyright:Copyright (c) 2018, 1245811923@qq.com All Rights Reserved.
 */
public class LoginLogInterceptor implements HandlerInterceptor{

	public static final Logger LOGGER = Logger.getLogger(LoginLogInterceptor.class); 
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		StringBuffer record = new StringBuffer(
				"尝试登录-\rIP地址:"+request.getRemoteHost()+
				"\r浏览器:"+request.getHeader("User-Agent")+
				"\r发起连接地址:"+request.getHeader("referer")+
				"\r控制器:"+handler.toString()
		);
		LOGGER.info(record);
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		if(!(modelAndView.getViewName().equals("/admin/index.jsp"))) {
			LOGGER.info("登录失败-请求的IP地址:"+request.getRemoteAddr());
		}
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		
	}

}
